package concurrent;

import java.util.concurrent.Semaphore;

public class PrintFoobarAlternately1115 {
    class FooBar {
        private int n;
        Semaphore sem1;
        Semaphore sem2;

        public FooBar(int n) {
            this.n = n;
            sem1 = new Semaphore(1);
            sem2 = new Semaphore(0);
        }

        public void foo(Runnable printFoo) throws InterruptedException {
            for (int i = 0; i < n; i++) {
                sem1.acquire(1);
                // printFoo.run() outputs "foo". Do not change or remove this line.
                printFoo.run();
                sem2.release(1);
            }
        }

        public void bar(Runnable printBar) throws InterruptedException {
            for (int i = 0; i < n; i++) {
                sem2.acquire(1);
                // printBar.run() outputs "bar". Do not change or remove this line.
                printBar.run();
                sem1.release(1);
            }
        }
    }
}
